<template>
  <!--订单页面-->
  <div class="guibinting order">
    <div class="search">
      <input type="text" placeholder="搜索名称/关键字" v-model="content">
      <img src="../../assets/search.png" alt="" width="16" @click="goSearch(activeIndex,content)">
    </div>
    <wv-tabs v-model="activeIndex" class="container" @click="getOrderData(activeIndex)">
      <wv-tab title="全部">
        <div v-for="item in  orderList" @click="goAllorderdetail(item.orderId)" class="msg">
          <div class="order_list" v-for="orderDetail  in  item.orderdetailList">
            <div class="weui-media-box weui-media-box_appmsg">
              <div class="weui-media-box__hd">
                <img :src="orderDetail.coverImg | imgPathFilter" alt="" class="weui-media-box__thumb">
              </div>
              <div class="weui-media-box__bd">
                <h4 class="weui-media-box__title">{{orderDetail.goodsName}} <span v-if="orderDetail.orderType==5"
                                                                                  class="zhuanka">转卡</span></h4>
                <p class="weui-media-box__desc">{{orderDetail.unitPrice}}元</p>
              </div>
              <div v-if="orderDetail.orderType==2 || orderDetail.orderType==3"
                   @click.stop="goAppointmentDetail(orderDetail.appointment,item.orderId)">
                <span class="yuyueing " v-if="orderDetail.appointment.appointmentState==1">预约中</span>
                <span class="yuyueing fail" v-if="orderDetail.appointment.appointmentState==3">预约失败</span>
                <span class="yuyueing suc" v-if="orderDetail.appointment.appointmentState==2">预约成功</span>
              </div>
              <span class="num">×{{orderDetail.buyNum}}</span>
            </div>
            <div class="status" v-if="item.orderStatus==2">
              <p v-if="orderDetail.categoryCode=='vipcard_1'">
                <button class="weui-btn weui-btn_plain-default" v-if="item.orderStatus==2"
                        @click.stop="goAgainBuy(orderDetail)">再次购买
                </button>
              </p>
              <p v-else-if="orderDetail.categoryCode=='insurance'">
                <button class="weui-btn weui-btn_plain-default" v-if="item.orderStatus==2"
                        @click.stop="goAgainBuy(orderDetail)">再次购买
                </button>
                <span class="yuyueing "
                      @click.stop="goLookDelayriskDetail(orderDetail.delayrisk.delayriskId)">查看详情</span>
                <wv-button type="default" :plain="true" @click.stop="goCompensate(orderDetail.delayrisk.delayriskId)"
                           v-if="orderDetail.delayrisk.isApply==1  "> 理赔
                </wv-button>
                <wv-button type="default" :plain="true"
                           @click.stop="goLookCompensate(orderDetail.delayrisk.delayriskId)"
                           v-if="orderDetail.delayrisk.isApply==2 ">理赔详情
                </wv-button>
              </p>
              <p v-else>
                <button class="weui-btn weui-btn_plain-default" v-if="item.orderStatus==2"
                        @click.stop="goAgainBuy(orderDetail)">再次购买
                </button>
                <wv-button v-if="orderDetail.useState==1" type="default" :plain="true"
                           @click.stop="goUse(orderDetail.accountGoodsId)">去使用
                </wv-button>
              </p>
            </div>
          </div>
          <p class="all status" style="margin-bottom: 30px">
            <span class="money">总计{{item.totalPrice}}元</span>
            <span v-if="item.orderStatus==1">
              <span class="daifu">待付款</span>
              <wv-button type="default" :plain="true" @click.stop="goPay(item)">去付款</wv-button>
            </span>
            <span v-if="item.orderStatus==3">
              <span class="success">已退款</span>
            </span>
            <span v-if="item.orderStatus==2">
                <span class="success">交易成功</span>
            </span>
            <wv-button type="default" :plain="true" @click.stop="goOrderDel(item)"> 删除订单</wv-button>
          </p>
        </div>
        <p class="nomore">没有更多了</p>
      </wv-tab>

      <wv-tab title="待付款">
        <div v-for="item in  orderList" @click="goAllorderdetail(item.orderId)" to="orderdetail" tag="div" class="msg">
          <div class="order_list" v-for="orderDetail  in  item.orderdetailList">
            <div class="weui-media-box weui-media-box_appmsg">
              <div class="weui-media-box__hd">
                <img :src="orderDetail.coverImg | imgPathFilter" alt="" class="weui-media-box__thumb">
              </div>
              <div class="weui-media-box__bd">
                <h4 class="weui-media-box__title">{{orderDetail.goodsName}} <span v-if="orderDetail.orderType==5"
                                                                                  class="zhuanka">转卡</span>
                  <div v-if="orderDetail.orderType==2 || orderDetail.orderType==3"
                       @click.stop="goAppointmentDetail(orderDetail.appointment,item.orderId)">
                    <span class="yuyueing " v-if="orderDetail.appointment.appointmentState==1">查看详情</span>
                    <span class="yuyueing fail" v-if="orderDetail.appointment.appointmentState==3">预约失败</span>
                    <span class="yuyueing suc" v-if="orderDetail.appointment.appointmentState==2">预约成功</span>
                  </div>
                </h4>
                <p class="weui-media-box__desc">{{orderDetail.unitPrice}}元</p>
              </div>

              <span class="num">×{{orderDetail.buyNum}}</span>
            </div>
            <div class="status" v-if="item.orderStatus==2">
              <p v-if="orderDetail.categoryCode=='vipcard_1'">
                <button class="weui-btn weui-btn_plain-default" v-if="item.orderStatus==2"
                        @click.stop="goAgainBuy(orderDetail)">再次购买
                </button>
              </p>
              <p v-else-if="orderDetail.categoryCode=='insurance'">
                <button class="weui-btn weui-btn_plain-default" v-if="item.orderStatus==2"
                        @click.stop="goAgainBuy(orderDetail)">再次购买
                </button>
                <span class="yuyueing "
                      @click.stop="goLookDelayriskDetail(orderDetail.delayrisk.delayriskId)">查看详情</span>
                <wv-button type="default" :plain="true" @click.stop="goCompensate(orderDetail.delayrisk.delayriskId)"
                           v-if="orderDetail.delayrisk.isApply==1  "> 理赔
                </wv-button>
                <wv-button type="default" :plain="true"
                           @click.stop="goLookCompensate(orderDetail.delayrisk.delayriskId)"
                           v-if="orderDetail.delayrisk.isApply==2 ">理赔详情
                </wv-button>
              </p>
              <p v-else>
                <button class="weui-btn weui-btn_plain-default" v-if="item.orderStatus==2"
                        @click.stop="goAgainBuy(orderDetail)">再次购买
                </button>
                <wv-button v-if="orderDetail.useState==1" type="default" :plain="true"
                           @click.stop="goUse(orderDetail.accountGoodsId)">去使用
                </wv-button>
              </p>
            </div>
          </div>
          <p class="all status" style="margin-bottom: 30px">
            <span class="money">总计{{item.totalPrice}}元</span>
            <span v-if="item.orderStatus==1">
              <span class="daifu">待付款</span>
              <wv-button type="default" :plain="true" @click.stop="goPay(item)">去付款</wv-button>
              <wv-button type="default" :plain="true" @click.stop="goOrderDel(item)"> 删除订单</wv-button>
            </span>
            <span v-if="item.orderStatus==3">
                 <span class="success">已退款</span>
            </span>
            <span v-if="item.orderStatus==2">
                 <span class="success">交易成功</span>
            </span>
          </p>
        </div>
        <p class="nomore">没有更多了</p>
      </wv-tab>
      <wv-tab title="已完成">
        <div v-for="item in  orderList" @click="goAllorderdetail(item.orderId)" to="orderdetail" tag="div" class="msg">
          <div class="order_list" v-for="orderDetail  in  item.orderdetailList">
            <div class="weui-media-box weui-media-box_appmsg">
              <div class="weui-media-box__hd">
                <img :src="orderDetail.coverImg | imgPathFilter" alt="" class="weui-media-box__thumb">
              </div>
              <div class="weui-media-box__bd">
                <h4 class="weui-media-box__title">{{orderDetail.goodsName}} <span v-if="orderDetail.orderType==5"
                                                                                  class="zhuanka">转卡</span></h4>
                <p class="weui-media-box__desc">{{orderDetail.unitPrice}}元</p>
              </div>
              <div v-if="orderDetail.orderType==2 || orderDetail.orderType==3"
                   @click.stop="goAppointmentDetail(orderDetail.appointment,item.orderId)">
                <span class="yuyueing " v-if="orderDetail.appointment.appointmentState==1">预约中</span>
                <span class="yuyueing fail" v-if="orderDetail.appointment.appointmentState==3">预约失败</span>
                <span class="yuyueing suc" v-if="orderDetail.appointment.appointmentState==2">预约成功</span>
              </div>
              <span class="num">×{{orderDetail.buyNum}}</span>
            </div>
            <div class="status" v-if="item.orderStatus==2">
              <p v-if="orderDetail.categoryCode=='vipcard_1'">
                <button class="weui-btn weui-btn_plain-default" v-if="item.orderStatus==2"
                        @click.stop="goAgainBuy(orderDetail)">再次购买
                </button>
              </p>
              <p v-else-if="orderDetail.categoryCode=='insurance'">
                <button class="weui-btn weui-btn_plain-default" v-if="item.orderStatus==2"
                        @click.stop="goAgainBuy(orderDetail)">再次购买
                </button>
                <span class="yuyueing "
                      @click.stop="goLookDelayriskDetail(orderDetail.delayrisk.delayriskId)">查看详情</span>
                <wv-button type="default" :plain="true" @click.stop="goCompensate(orderDetail.delayrisk.delayriskId)"
                           v-if="orderDetail.delayrisk.isApply==0  "> 理赔
                </wv-button>
                <wv-button type="default" :plain="true"
                           @click.stop="goLookCompensate(orderDetail.delayrisk.delayriskId)"
                           v-if="orderDetail.delayrisk.isApply==2  ">理赔详情
                </wv-button>
              </p>
              <p v-else>
                <button class="weui-btn weui-btn_plain-default" v-if="item.orderStatus==2"
                        @click.stop="goAgainBuy(orderDetail)">再次购买
                </button>
                <wv-button v-if="orderDetail.useState==1" type="default" :plain="true"
                           @click.stop="goUse(orderDetail.accountGoodsId)">去使用
                </wv-button>
              </p>
            </div>
          </div>
          <p class="all status" style="margin-bottom: 30px">
            <span class="money">总计{{item.totalPrice}}元</span>
            <span v-if="item.orderStatus==1">
              <span class="daifu">待付款</span>
              <wv-button type="default" :plain="true" @click.stop="goPay(item)">去付款</wv-button>
            </span>
            <span v-if="item.orderStatus==3">
                 <span class="success">已退款</span>
                 <wv-button type="default" :plain="true" @click.stop="goOrderDel(item)"> 删除订单</wv-button>
            </span>
            <span v-if="item.orderStatus==2">
                 <span class="success">交易成功</span>
                 <wv-button type="default" :plain="true" @click.stop="goOrderDel(item)"> 删除订单</wv-button>
            </span>
          </p>
        </div>
        <p class="nomore">没有更多了</p>
      </wv-tab>
    </wv-tabs>
    <wv-tabbar :fixed=true>
      <wv-tabbar-item to="/index">
        <img class="weui-tabbar__icon" src="../../assets/selecthome.png" slot="icon">
        主页
      </wv-tabbar-item>
      <wv-tabbar-item to="/saoma">
        <img class="weui-tabbar__icon" src="../../assets/saoma.png" slot="icon">
        扫码验证
      </wv-tabbar-item>
      <wv-tabbar-item to="/shangcheng">
        <img class="weui-tabbar__icon" src="../../assets/no.png" slot="icon">
        商城
      </wv-tabbar-item>
      <wv-tabbar-item to="/order" is-on>
        <img class="weui-tabbar__icon" src="../../assets/selectfour.png" slot="icon">
        订单
      </wv-tabbar-item>
      <wv-tabbar-item to="/my">
        <img class="weui-tabbar__icon" src="../../assets/my.png" slot="icon"> 我的
      </wv-tabbar-item>
    </wv-tabbar>
  </div>
</template>

<script type="text/ecmascript-6">
  import {getOrderList, orderGoPay, orderDel} from '../../../test/unit/http'
  import {getStore} from '../../../test/unit/storage'
  import {wxApi} from '../../../test/unit/flyme_weixin'
  import {Toast} from 'we-vue'

  export default {
    name: 'order',
    props: {},
    data() {
      return {
        activeIndex: 0,
        thumb: require('../../assets/lipinjinka.png'),
        orderList: [],
        content: ''
      };
    },
    methods: {
      goPay(order) {
        let utils = this.$myUtils;
        let baseUrl = utils.getBaseUrl();
        console.log(baseUrl);
        let payType = order.payType;
        if (payType == 'PAY_WX') {
          baseUrl = baseUrl + "/#/payresult?";
        }
        if (payType == 'PAY_WX_YINLIAN') {
          baseUrl = baseUrl + "/#/payresultgold";
        }
        order['busType'] = 'ORDER';
        order['frontUrl'] = baseUrl;
        if (payType == 'PAY_UNDERLINE') {
          this.$router.push({path: 'downloadoffer', query: {orderId: order.orderId}});
        } else {
          orderGoPay(order).then(res => {
            console.log(res);
            if (res.code == 100) {
              window.location.href = res.object.url;
            }
          })
        }
      },
      goSearch(activeIndex, content) {
        let params = {goodsName: content};
        if (activeIndex != 0) {
          params['orderStatus'] = activeIndex;
        }
        this.getOrderListData(params)
      },
      getOrderData(activeIndex) {
        let params = {};
        if (activeIndex != 0) {
          params['orderStatus'] = activeIndex;
        }
        this.getOrderListData(params)
      },
      goGoodsDetail(goodsId, categoryCode) {
        if (categoryCode == 'vipcard_2' || categoryCode == 'vipcard_1') {
          this.$router.push({path: 'jinkadetail', query: {goodsId: goodsId}});
          return;
        }
        if (categoryCode == 'viproom') {
          this.$router.push({path: 'guibindetail', query: {goodsId: goodsId, entrySource: 'shop'}});
          return;
        }
        if (categoryCode == 'viponly') {
          this.$router.push({path: 'tongdaodetail', query: {goodsId: goodsId, entrySource: 'shop'}});
          return;
        }
        if (categoryCode == 'coupons') {
          this.$router.push({path: 'youhuiquan', query: {goodsId: goodsId}});
          return;
        }
        if (categoryCode == 'insurance') {
          this.$router.push({path: 'baozhangdetail', query: {goodsId: goodsId}});
          return;
        }
        this.$router.push({path: 'goodsdetail', query: {goodsId: goodsId}})
      },
      goAgainBuy(orderDetail) {
        console.log(orderDetail);
        this.goGoodsDetail(orderDetail.goodsId, orderDetail.categoryCode);
      },
      goAppointmentDetail(appointment, orderId) {
        this.$router.push({
          path: 'orderdetailyuyue',
          query: {appointmentId: appointment.appointmentId, orderId: orderId}
        })
      },
      goUse(accountGoodsId) {
        console.log(accountGoodsId)
      },
      goLookDelayriskDetail(delayriskId) {
        this.$router.push({path: 'lipei', query: {delayriskId: delayriskId}})
      },
      goCompensate(delayriskId) {
        this.$router.push({path: 'shenqinglipei', query: {delayriskId: delayriskId}})
      },
      goLookCompensate(delayriskId) {
        this.$router.push({path: 'lipeidetail', query: {delayriskId: delayriskId}})
      },
      goAllorderdetail(orderId) {
        this.$router.push({path: 'allorderdetail', query: {orderId: orderId}})
      },
      getOrderListData(params) {
        getOrderList(params).then(res => {
          console.log(res);
          if (res.code == 100) {
            this.orderList = res.object.rows;
          }
        })
      },
      goOrderDel(order) {
        console.log(order);
        let activeIndex = this.activeIndex;
        orderDel({orderId: order.orderId}).then(res => {
          if (res.code == 100) {
            this.getOrderData(activeIndex);
          } else {
            Toast.text(res.info)
          }
        })
      }
    }
    ,
    created() {
      wxApi.init();
      let orderStatus = this.$route.query.orderStatus;
      if (this.$myUtils.isNull(orderStatus)) {
        this.getOrderData(this.activeIndex)
      } else {
        this.activeIndex = orderStatus;
        this.getOrderData(this.activeIndex)
      }
    }
    ,
    components: {}
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less" rel="stylesheet/less">
  .order_list {
    position: relative;

    .zhuanka {
      color: #ff8551;
    }

  }

  .weui-panel:before {
    border-top: none;
  }

  .nomore {
    color: #999;
    padding: 10px;
  }

  input::-webkit-input-placeholder {
    color: #999;
    font-size: 14px;
  }

  .search {
    position: relative;
    width: 95%;
    margin: 10px auto;

    input {
      width: 90%;
      border: 1px solid #eee;
      box-shadow: 0 3px 4px rgba(17, 17, 17, 0.05);
      border-radius: 7px;
      height: 25px;
      line-height: 25px;
      padding: 10px;
      color: #111;
    }

    img {
      position: absolute;
      right: 20px;
      top: 15px;
    }
  }

  .msg {
    position: relative;
    border-bottom: 1px solid #eee;
    padding: 0 10px 10px;

    .order_list:not(:last-of-type) {
      padding-bottom: 10px;
      border-bottom: 1px solid #eee;
    }

    .all {
      border-top: 1px solid #eee;
      padding-top: 10px;
      margin-top: 10px;

      .money {
        color: #ff8551;
        float: left;
        margin-top: 5px;
      }
    }

    .state {
      color: #e96774;
      font-size: 12px;
      margin-left: 5px;
    }

    .yuyueing {
      font-size: 10px;
      color: #fff;
      background-color: #ccc;
      border-radius: 25px;
      padding: 4px 15px;
      margin-left: 5px;
      position: absolute;
      top: 20px;
      right: 0;
    }

    .suc {
      background-color: #3095f0;
    }

    .fail {
      background-color: #e96775;
    }

    .num {
      font-size: 15px;
      color: #666;
      margin-top: 55px;
    }

    .status {
      font-size: 12px;
      text-align: right;
      padding-right: 15px;

      .success {
        color: #999;
      }

      .daifu {
        color: #ff8551;
      }

      button {
        border-radius: 25px;
        display: inline-block;
        width: 72px;
        font-size: 15px;
        color: #111;
        margin-left: 10px;
        line-height: 1.6
      }
    }
  }

  .order {
    margin-bottom: 50px;
  }

  .weui-btn {
    padding: 0 5px;
  }

  .weui-btn + .weui-btn {
    margin-top: 0;
  }
</style>
